package com.fdb.efp.limit.service.impl;


import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

import com.fdb.efp.limit.service.dao.LmtIndivAppDao;
import com.fdb.efp.limit.service.dao.LmtPrdContDao;
import com.fdb.efp.limit.service.dao.LmtPrelistDao;
import com.fdb.efp.limit.service.dao.LmtShowInfoDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.fdb.basic.framework.core.base.AbstractFrameworkService;
import com.fdb.basic.framework.core.base.DataOptionalAuthorityDao;
import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.bean.SedSynCusManageVO;
import com.fdb.basic.framework.core.constant.BaseConstant;
import com.fdb.basic.framework.core.exception.PauseException;
import com.fdb.basic.framework.core.exception.ValidateException;
import com.fdb.basic.framework.core.util.DateTool;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.basic.framework.core.util.TimeUtil;
import com.fdb.efp.console.service.facade.PubSysInfoService;
import com.fdb.efp.limit.common.LimitConstant;
import com.fdb.efp.limit.service.domain.LmtPrdCont;
import com.fdb.efp.limit.service.domain.LmtPrelist;
import com.fdb.efp.limit.service.domain.LmtShowInfo;
import com.fdb.efp.limit.service.facade.LmtPrdContService;
import com.fdb.efp.limit.service.vo.LmtPrdContVO;
import com.fdb.efp.limit.service.vo.LmtPrelistVO;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDate;
import java.util.ArrayList;

/**
 * <p>ServiceImpl: 授信协议表<p>
 * @author chenqm1
 * @date 2018-05-09
 */
@Service("lmtPrdContService")
public class LmtPrdContServiceImpl extends AbstractFrameworkService implements LmtPrdContService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(LmtPrdContServiceImpl.class);

	@Autowired
	private LmtPrdContDao lmtPrdContDao;
	
	@Autowired
	private LmtIndivAppDao lmtIndivAppDao;
	
	@Autowired
	private LmtShowInfoDao lmtShowInfoDao;
	
	@Autowired
	private LmtPrelistDao lmtPrelistDao;
	
	@Autowired
	@Qualifier("pubSysInfoService")
	private PubSysInfoService pubSysInfoService;
	
	/**
	 * 产品协议表插入记录
	 */
	public int insertLmtPrdCont(LmtPrdContVO lmtPrdContVo) {
		logger.info("当前新增参数为:"+ JSON.toJSONString(lmtPrdContVo));
		int num = 0;
		try {
			LmtPrdCont lmtPrdCont = new LmtPrdCont();
			beanCopy(lmtPrdContVo, lmtPrdCont);
			/*LmtIndivApp lmtIndivApp = new LmtIndivApp();
			lmtIndivApp.setLmtSerno(lmtPrdContVo.getLmtApplySeq());
			lmtIndivApp.setCertType(lmtPrdContVo.getCertType());
			lmtIndivApp.setCertCode(lmtPrdContVo.getCertCode());
			//查询对应授信申请表数据
			lmtIndivApp = lmtIndivAppDao.queryByPk(lmtIndivApp);
			if (lmtIndivApp==null) {
				throw new ValidateException("无对应授信申请数据");
			}
			logger.info("对应授信申请表数据为: "+JSON.toJSONString(lmtIndivApp));
			
			//查询白名单数据
			LmtPrelist lmtPrelist = lmtPrelistDao.queryLmtPrelistByLmtSerno(lmtPrdContVo.getLmtApplySeq());
			BigDecimal waitActAmt = Objects.isNull(lmtPrelist)?null:lmtPrelist.getWaitActAmt();
			//mod by 20181007 首次申请额度未激活时提额成功
			BigDecimal lmtAmt = Objects.isNull(waitActAmt)?lmtIndivApp.getApproveAmt():waitActAmt;
			
			LmtPrdCont lmtPrdCont = new LmtPrdCont();
			lmtPrdCont = (LmtPrdCont)beanCopy(lmtPrdContVo, lmtPrdCont);
			if (lmtPrdContDao.queryByPk(lmtPrdCont)!=null) {
				throw new ValidateException("表中已有该流水号数据");
			}
			lmtPrdCont = (LmtPrdCont)beanCopy(lmtIndivApp, lmtPrdCont);//产品协议bean基本信息复制
			lmtPrdCont.setLoanUse(lmtIndivApp.getLoanUseType());//贷款用途类型
			lmtPrdCont.setRateY(lmtIndivApp.getApproveRateY());
			lmtPrdCont.setStartDate(this.getOpenday());
			lmtPrdCont.setSignDate(this.getOpenday());
			lmtPrdCont.setLmtStatus(LimitConstant.LMT_STATE_00);
			lmtPrdCont.setApproveStatus(BaseConstant.APPR_STATUS_000);
			lmtPrdCont.setTerm(lmtIndivApp.getApplyTerm());
			lmtPrdCont.setLmtAmt(lmtAmt);
			lmtPrdCont.setCreateTime(DateTool.getCurrentDateTime());
			lmtPrdCont.setLastUpdateTime(lmtPrdCont.getCreateTime());
			//插入授信协议表
			logger.info("新增授信协议数据为: "+JSON.toJSONString(lmtPrdCont));*/
			num = lmtPrdContDao.insertLmtPrdCont(lmtPrdCont);
			logger.info("当前新增数据条数为:"+ num);
			
			/*//更新lmt_show_info
	        LmtShowInfo lmtShowInfo = new LmtShowInfo();
	        lmtShowInfo.setLmtSerno(lmtPrdContVo.getLmtApplySeq());
	        lmtShowInfo.setLmtContNo(lmtPrdCont.getLmtContNo());
	        lmtShowInfo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
	        lmtShowInfoDao.updateShowInfoByLmtSerno(lmtShowInfo);*/
		} catch (ValidateException e) {
			logger.error(e.getMessage(), e);
			num = -1;
		}catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(LmtPrdContVO inLmtPrdContVo) {
		logger.info("当前删除数据条件为:"+ JSON.toJSONString(inLmtPrdContVo));
		int num = 0;
		try {
			LmtPrdCont lmtPrdCont = new LmtPrdCont();
			lmtPrdCont = (LmtPrdCont)beanCopy(inLmtPrdContVo, lmtPrdCont);
			num = lmtPrdContDao.deleteByPk(lmtPrdCont);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.info("根据条件:"+ JSON.toJSONString(inLmtPrdContVo) +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(LmtPrdContVO inLmtPrdContVo) {
		logger.info("当前修改数据为:"+ JSON.toJSONString(inLmtPrdContVo));
		int num = 0;
		try {
			LmtPrdCont lmtPrdCont = new LmtPrdCont();
			lmtPrdCont = (LmtPrdCont)beanCopy(inLmtPrdContVo, lmtPrdCont);
			lmtPrdCont.setLastUpdateTime(DateTool.getCurrentDateTime());
			num = lmtPrdContDao.updateByPk(lmtPrdCont);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.info("根据条件:"+ JSON.toJSONString(inLmtPrdContVo) +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public LmtPrdContVO queryByPk(LmtPrdContVO inLmtPrdContVo) {
		
		logger.info("当前查询参数信息为:"+ JSON.toJSONString(inLmtPrdContVo));
		try {
			LmtPrdCont queryLmtPrdCont = new LmtPrdCont();
			queryLmtPrdCont = (LmtPrdCont)beanCopy(inLmtPrdContVo, queryLmtPrdCont);
			LmtPrdCont queryRslLmtPrdCont = lmtPrdContDao.queryByPk(queryLmtPrdCont);
			if (Objects.nonNull(queryRslLmtPrdCont)) {
				LmtPrdContVO outLmtPrdContVo = beanCopy(queryRslLmtPrdCont, new LmtPrdContVO());
				logger.info("当前查询结果为:"+ JSON.toJSONString(outLmtPrdContVo));
				return outLmtPrdContVo;
			} else {
				logger.info("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}
	
	/**
	 * 根据对象查询所有数据，不分页
	 * @param lmtPrdCont
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<LmtPrdContVO> queryLmtPrdList(LmtPrdContVO lmtPrdContVo) {
		logger.info("当前查询参数信息为:"+JSON.toJSONString(lmtPrdContVo));
		List<LmtPrdCont> lmtPrdConts = lmtPrdContDao.queryLmtPrdList(lmtPrdContVo);
		if (Objects.isNull(lmtPrdConts)) {
			logger.info("当前查询结果集为空");
			return null;
		}
		logger.info("当前查询结果集数量为:"+lmtPrdConts.size());
		List<LmtPrdContVO> list = new ArrayList<>();
		try {
			pageSet(lmtPrdConts, lmtPrdContVo);
			list = (List<LmtPrdContVO>) beansCopy(lmtPrdConts, LmtPrdContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<LmtPrdContVO> queryAllOwner(LmtPrdContVO lmtPrdContVo) {

		logger.info("当前查询本人所属数据信息的参数信息为:"+JSON.toJSONString(lmtPrdContVo));
		List<LmtPrdContVO> list = new ArrayList<>();
		try {
			List<LmtPrdCont> lmtPrdConts = lmtPrdContDao.queryAllOwnerByPage(lmtPrdContVo);
			if (Objects.isNull(lmtPrdConts)) {
				logger.info("当前查询本人所属数据信息的结果集为空");
				return null;
			}
			logger.info("当前查询本人所属数据信息的结果集数量为:"+ lmtPrdConts.size());
			pageSet(lmtPrdConts, lmtPrdContVo);
			list = (List<LmtPrdContVO>) beansCopy(lmtPrdConts, LmtPrdContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<LmtPrdContVO> queryAllCurrOrg(LmtPrdContVO lmtPrdContVo) {
		logger.info("当前查询本人所属机构数据信息的参数信息为:"+JSON.toJSONString(lmtPrdContVo));
		List<LmtPrdCont> lmtPrdConts = lmtPrdContDao.queryAllCurrOrgByPage(lmtPrdContVo);
		if (Objects.isNull(lmtPrdConts)) {
			logger.info("当前查询本人所属数据信息的结果集为空");
			return null;
		}
		logger.info("当前查询本人所属机构数据信息的结果集数量为:"+lmtPrdConts.size());
		List<LmtPrdContVO> list = new ArrayList<>();
		try {
			pageSet(lmtPrdConts, lmtPrdContVo);
			list = (List<LmtPrdContVO>) beansCopy(lmtPrdConts, LmtPrdContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<LmtPrdContVO> queryAllCurrDownOrg(LmtPrdContVO lmtPrdContVo) {

		logger.info("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<LmtPrdCont> lmtPrdConts = lmtPrdContDao.queryAllCurrDownOrgByPage(lmtPrdContVo);
		if (Objects.isNull(lmtPrdConts)) {
			logger.info("当前查询本人所属数据信息的结果集为空");
			return null;
		}
		logger.info("当前查询本人所属机构及以下数据信息的结果集数量为:"+ lmtPrdConts.size());
		List<LmtPrdContVO> list = new ArrayList<>();
		try {
			pageSet(lmtPrdConts, lmtPrdContVo);
			list = (List<LmtPrdContVO>) beansCopy(lmtPrdConts, LmtPrdContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}

	@Override
	public List<LmtPrdContVO> queryAllCurrOwnerPrd(LmtPrdContVO lmtPrdContVo) {
		logger.info("当前产品种类权限下的参数信息为:" + lmtPrdContVo.toString());
		List<LmtPrdCont> lmtPrdConts = lmtPrdContDao.queryAllCurrOwnerPrdByPage(lmtPrdContVo);
		if (Objects.isNull(lmtPrdConts)) {
			logger.info("当前产品种类权限下的结果集为空！");
			return null;
		}
		logger.info("当前产品种类权限下的结果集数量为:"+ lmtPrdConts.size());
		List<LmtPrdContVO> list = new ArrayList<>();
		try {
			pageSet(lmtPrdConts, lmtPrdContVo);
			list = (List<LmtPrdContVO>) beansCopy(lmtPrdConts, LmtPrdContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	/**
	 * 额度激活进度查询
	 * @param lmtPrdContVO
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<LmtPrdContVO> queryPrdContProcess(LmtPrdContVO lmtPrdContVO) {
		logger.info("当前输入的参数为: "+JSON.toJSONString(lmtPrdContVO));
		List<LmtPrdContVO> list = null;
		String[] approveStatusArr = null;
		if (StringUtil.isEmpty(lmtPrdContVO.getApproveStatus())) {
			approveStatusArr= new String[]{BaseConstant.APPR_STATUS_111, BaseConstant.APPR_STATUS_997, BaseConstant.APPR_STATUS_998};
		}else {
			approveStatusArr= new String[]{lmtPrdContVO.getApproveStatus()};				
			};	
			try {
				List<LmtPrdCont>  resultList= lmtPrdContDao.queryPrdContProcess(lmtPrdContVO, approveStatusArr);
				logger.info("额度激活进度查询结果集为: "+JSON.toJSONString(resultList));
				pageSet(resultList, lmtPrdContVO);
				list = (List<LmtPrdContVO>)beansCopy(resultList, LmtPrdContVO.class);
			} catch (Exception e) {
				logger.error("查询发生异常!",e);
			}
		return list;
	}

	/**
	 * 授信协议 状态修改
	 */
	@Override
	public int updatePrdContStatus(LmtPrdContVO lmtPrdContVo) {
		logger.info("当前输入参数为: "+JSON.toJSONString(lmtPrdContVo));
		int num =0;
		try {
			LmtPrdCont lmtPrdCont = new LmtPrdCont();
			lmtPrdCont = (LmtPrdCont)beanCopy(lmtPrdContVo, lmtPrdCont);
			lmtPrdCont.setLastUpdateTime(DateTool.getCurrentDateTime());
			num = lmtPrdContDao.updatePrdContStatus(lmtPrdCont);
			logger.info("当前修改条数为: "+num);
		} catch (Exception e) {
			num = -1;
			logger.info("修改失败!",e);
		}
		return num;
	}
	
    @Override
    public boolean signPrdCont(String lmtContNo) throws Exception {
        logger.info("开始签订授信协议，lmtContNo="+lmtContNo);
        try {
            LmtPrdCont lmtPrdCont = new LmtPrdCont();
            lmtPrdCont.setLmtContNo(lmtContNo);
            logger.info("查询授信协议参数为: "+JSON.toJSONString(lmtPrdCont));
            LmtPrdCont lmtPrdContFromDB = lmtPrdContDao.queryByPk(lmtPrdCont);
            logger.info("查询授信协议结果为: "+JSON.toJSONString(lmtPrdContFromDB));  
            if(Objects.nonNull(lmtPrdContFromDB)) {
                //多借款合同的情况，不需重复签订 mod by 20180923
                if(BaseConstant.APPR_STATUS_997.equals(lmtPrdContFromDB.getApproveStatus())) {
                    return true;
                }
                String openday = pubSysInfoService.getOpenday(BaseConstant.SYS_ID_1001);//营业日期
                BigDecimal term = lmtPrdContFromDB.getTerm();//期限
                int term_ = term==null?0:term.intValue();
                String termType = lmtPrdContFromDB.getTermType();//期限类型
                
                String expireDate = openday;
                if(BaseConstant.TERM_TYPE_DAY.equals(termType)) {
                    expireDate = TimeUtil.addDay(openday, term_);
                } else if(BaseConstant.TERM_TYPE_MONTH.equals(termType)) {
                    expireDate = TimeUtil.addMonth(openday, term_);
                } else if(BaseConstant.TERM_TYPE_YEAR.equals(termType)) {
                    expireDate = TimeUtil.addYear(openday, term_);
                }
                
                lmtPrdCont.setStartDate(openday);//开始日期
                lmtPrdCont.setExpireDate(TimeUtil.addDay(expireDate, -1));//到期日期 算头不算尾 减1天
                lmtPrdCont.setSignDate(openday);//签订日期
                lmtPrdCont.setLmtStatus(LimitConstant.LMT_STATE_01);//授信状态
                lmtPrdCont.setApproveStatus(BaseConstant.APPR_STATUS_997);//审批状态
                //lmtPrdCont.setLastUpdateUser(lastUpdateUser);
                lmtPrdCont.setLastUpdateTime(TimeUtil.getCurrentDateTime());
                
                int count = lmtPrdContDao.updateByPk(lmtPrdCont);
                LmtPrelist lmtPrelist = new LmtPrelist();
                lmtPrelist.setLmtContNo(lmtContNo);
                LmtPrelistVO lmtPrelistVO = new LmtPrelistVO();
                lmtPrelistVO = beanCopy(lmtPrelist,lmtPrelistVO);
                
                logger.info(lmtContNo+"签订授信协议结果：" + (count==1?"success":"failure"));
                if(count!=1) {
                    throw new PauseException("签订授信协议失败，授信协议号：" + lmtContNo);
                }
                
                //更新lmt_show_info表
                LmtShowInfo lmtShowInfo = new LmtShowInfo();
                lmtShowInfo.setLmtSerno(lmtPrdContFromDB.getLmtApplySeq());//授信申请流水号
                lmtShowInfo.setLmtContNo(lmtPrdContFromDB.getLmtContNo());//授信协议编号
                lmtShowInfo.setLmtExpireDate(lmtPrdCont.getExpireDate());//授信到期日
                lmtShowInfo.setLastUpdateTime(TimeUtil.getCurrentDateTime());//最后更新时间
                lmtShowInfoDao.updateShowInfoByLmtSerno(lmtShowInfo);
                
            } else {
                logger.error("根据授信协议号无法获取到授信协议信息，授信协议号：" + lmtContNo);
                throw new PauseException("根据授信协议号无法获取到授信协议信息，授信协议号：" + lmtContNo);
            }
        } catch (Exception e) {
            logger.error("签订授信协议出现异常！", e);
            throw new PauseException(e.getMessage());
        }
        
        return true;
    }

	@SuppressWarnings("unchecked")
	@Override
	public List<LmtPrdContVO> queryLmtPrdContsByLegal(LmtPrdContVO lmtPrdContVo) {

		logger.info("当前查询当前登录法人机构下的所属数据信息的参数信息为:"+JSON.toJSONString(lmtPrdContVo));
		List<LmtPrdContVO> list = new ArrayList<>();
		try {
			List<LmtPrdCont> lmtPrdConts = lmtPrdContDao.queryLmtPrdContsByLegalByPage(lmtPrdContVo);
			if (Objects.isNull(lmtPrdConts)) {
				logger.info("当前查询当前登录法人机构下所属数据信息的结果集为空");
				return null;
			}
			logger.info("当前查询当前登录法人机构下的所属数据信息的结果集数量为:"+ lmtPrdConts.size());
			pageSet(lmtPrdConts, lmtPrdContVo);
			list = (List<LmtPrdContVO>) beansCopy(lmtPrdConts, LmtPrdContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<LmtPrdContVO> queryLmtPrdContsForTerByLegal(LmtPrdContVO lmtPrdContVo) {

		logger.info("当前查询当前登录法人机构下的所属数据信息的参数信息为:"+JSON.toJSONString(lmtPrdContVo));
		List<LmtPrdContVO> list = new ArrayList<>();
		try {
			List<LmtPrdCont> lmtPrdConts = lmtPrdContDao.queryLmtPrdContsForTerByLegalByPage(lmtPrdContVo);
			if (Objects.isNull(lmtPrdConts)) {
				logger.info("当前查询当前登录法人机构下所属数据信息的结果集为空");
				return null;
			}
			logger.info("当前查询当前登录法人机构下的所属数据信息的结果集数量为:"+ lmtPrdConts.size());
			pageSet(lmtPrdConts, lmtPrdContVo);
			list = (List<LmtPrdContVO>) beansCopy(lmtPrdConts, LmtPrdContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public DataOptionalAuthorityDao getDataAuthorityDao() {
		return this.lmtPrdContDao;
	}

	@Override
	public LmtPrdContVO qryLmtPrdContByCondition(LmtPrdContVO lmtPrdContVo) {
		logger.info("查询数据的参数为:"+ JSON.toJSONString(lmtPrdContVo));
		LmtPrdContVO result = null;
		try {
			LmtPrdCont lmtPrdCont = new LmtPrdCont();
			lmtPrdCont = beanCopy(lmtPrdContVo, lmtPrdCont);
			lmtPrdCont = lmtPrdContDao.qryLmtPrdContByCondition(lmtPrdCont);
			if (Objects.nonNull(lmtPrdCont)) {
				result =  beanCopy(lmtPrdCont, new LmtPrdContVO());
				logger.debug("当前查询结果为:"+ JSON.toJSONString(result));
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		logger.info("根据条件:"+ JSON.toJSONString(lmtPrdContVo) + " 查询得的结果为："+ JSON.toJSONString(result));
		return result;
	}
	
	/**
	 * 失效 超过授信协议到期的记录 <br>
	 *
	 * 0.1:caoxin:2019/3/22 8:53: 失效 超过授信协议到期的记录
	 * @author caoxin
	 * @return 
	 * @since 2019/3/22 8:53
	 * @date 2019/3/22 8:53
	 * @version 0.1
	 */
	
	@Override
	public int inValidLmtProContOverExpireDate(String openDay) {
		LocalDate date = LocalDate.now();
		String msg1 = "开始失效超过授信协议到期的记录..."+date;
		logger.debug(msg1,"message {}");
		int num = 0;
		try {
			num = lmtPrdContDao.inValidLmtProContOverExpireDate(openDay);
		} catch (Exception e) {
			String msg2 = "失效超过授信协议到期的记录发生错误" + e;
			num = -1;
			logger.error(msg2,"message {}");
		}
		return num;
	}

	@Override
	public LmtPrdContVO qryLmtPrdContByLmtApplySeq(String lmtApplySeq) {
		logger.info("查询信息开始，参数授信申请流水号为："+lmtApplySeq );
		LmtPrdContVO result = null;
		try {
			LmtPrdCont lmtPrdCont = lmtPrdContDao.qryLmtPrdContByLmtApplySeq(lmtApplySeq);
			if (Objects.nonNull(lmtPrdCont)) {
				result =  beanCopy(lmtPrdCont, new LmtPrdContVO());
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		logger.info("根据授信申请流水号："+ lmtApplySeq + "，查询得的结果为："+ JSON.toJSONString(result));
		return result;
	}

	/**
	 * 
	 * 同步额度信息表有数据则更新，无则新增 <br>
	 * 0.1:ludayong:2019年5月10日 下午3:35:56:新增 <br>
	 * 
	 * @author ludayong
	 * @since 2019年5月10日 下午3:35:56
	 * @date 2019年5月10日 下午3:35:56
	 * @version 0.1
	 */
	@SuppressWarnings("unchecked")
	@Override
	public int insertOrUpdateLmtPrdCont(List<LmtPrdContVO> list) {
		LocalDate today = LocalDate.now();
		String msg1 = "【额度信息表】数据同步开始..." + today;
		logger.info(msg1, "message{}");
		List<LmtPrdCont> lmtPrdContList = new ArrayList<LmtPrdCont>();
		int num = 0;
		try {
			lmtPrdContList = (List<LmtPrdCont>) beansCopy(list, LmtPrdCont.class);
			//批量删除
			int delNum = lmtPrdContDao.batchDeleteLmtPrdContByLmtApplySeq(lmtPrdContList);
			num = lmtPrdContDao.batchInsertLmtPrdCont(lmtPrdContList);
			String num_msg = "【额度信息表】插入或更新数据处理结果:" + num;
			logger.info(num_msg, "message{}");
		} catch (Exception e) {
			String msg2 = "【额度信息表】插入或更新数据出现异常:" + e;
			logger.error(msg2, "message{}");
			num = -1;
		}
		return num;
	}
	
	/**
	 * 
	 * TODO <br>
	 * 0.1:wanglihua:2019年4月19日 上午11:03:23:TODO <br>
	 * @author wanglihua
	 * @since 2019年4月19日 上午11:03:23
	 * @date 2019年4月19日 上午11:03:23
	 * @version 0.1 初始为0.1,后续变更则修改
	 */
	public LmtPrdContVO queryContractEle(LmtPrdContVO lmtPrdContVO) {
		logger.info("当前查询参数信息为:"+ JSON.toJSONString(lmtPrdContVO));
		try {
			LmtPrdCont queryLmtPrdCont = new LmtPrdCont();
			queryLmtPrdCont = (LmtPrdCont)beanCopy(lmtPrdContVO, queryLmtPrdCont);
			LmtPrdCont queryRslLmtPrdCont = lmtPrdContDao.queryContractEle(queryLmtPrdCont);
			if (Objects.nonNull(queryRslLmtPrdCont)) {
				LmtPrdContVO outLmtPrdContVo = beanCopy(queryRslLmtPrdCont, new LmtPrdContVO());
				logger.info("当前查询结果为:"+ JSON.toJSONString(outLmtPrdContVo));
				return outLmtPrdContVo;
			} else {
				logger.info("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}
	
	/**
	 * 
	 * TODO 个人额度查询<br>
	 * @author wanglihua
	 * @since 2019年4月24日 上午9:51:39
	 * @date 2019年4月24日 上午9:51:39
	 */
	public LmtPrdContVO queryOneQuota(LmtPrdContVO lmtPrdContVO) {
		logger.info("当前查询参数信息为:"+ JSON.toJSONString(lmtPrdContVO));
		try {
			LmtPrdCont queryLmtPrdCont = new LmtPrdCont();
			queryLmtPrdCont = (LmtPrdCont)beanCopy(lmtPrdContVO, queryLmtPrdCont);
			LmtPrdCont queryRslLmtPrdCont = lmtPrdContDao.queryOneQuota(queryLmtPrdCont);
			if (Objects.nonNull(queryRslLmtPrdCont)) {
				LmtPrdContVO outLmtPrdContVo = beanCopy(queryRslLmtPrdCont, new LmtPrdContVO());
				logger.info("当前查询结果为:"+ JSON.toJSONString(outLmtPrdContVo));
				return outLmtPrdContVo;
			} else {
				logger.info("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 
	 * TODO 个人额度查询<br>
	 * @author wanglihua
	 * @since 2019年4月24日 上午9:51:39
	 * @date 2019年4月24日 上午9:51:39
	 */
	@SuppressWarnings("unchecked")
	public List<LmtPrdContVO> queryOneCus(LmtPrdContVO lmtPrdContVO) {
		logger.info("查询个人额度开始！参数：" + lmtPrdContVO);
		List<LmtPrdContVO> list = new ArrayList<>();
		String[] btchLmtStCd = null;
		try {
			
			if(!StringUtil.isNullorBank(lmtPrdContVO.getLmtStatus())) {
				btchLmtStCd = lmtPrdContVO.getLmtStatus().split(",");
			}
			LmtPrdCont queryLmtPrdCont = new LmtPrdCont();
			queryLmtPrdCont = (LmtPrdCont)beanCopy(lmtPrdContVO, queryLmtPrdCont);
			List<LmtPrdCont> queryLmtPrdConts = lmtPrdContDao.queryOneCus(queryLmtPrdCont,btchLmtStCd);
			if (Objects.isNull(queryLmtPrdConts)) {
				logger.info("当前查询个人额度的结果集为空");
				return null;
			}
			logger.info("当前查询个人额度信息的结果集数量为:"+ queryLmtPrdConts.size());
			list = (List<LmtPrdContVO>) beansCopy(queryLmtPrdConts, LmtPrdContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LmtPrdContVO> qryLmtPrdContByCust(LmtPrdContVO lmtPrdContVO) {
		logger.info("当前根据证件类型、证件号码查询的参数为："+ JSON.toJSONString(lmtPrdContVO));
		List<LmtPrdContVO> result = null;
		try {
			LmtPrdCont lmtPrdCont = new LmtPrdCont();
			lmtPrdCont = beanCopy(lmtPrdContVO, lmtPrdCont);
			List<LmtPrdCont> queryLmtPrdConts = lmtPrdContDao.qryLmtPrdContByCust(lmtPrdCont);
			if (Objects.isNull(queryLmtPrdConts)) {
				logger.info("根据证件类型、证件号码查询的结果集为空。");
				return result;
			}
			result = (List<LmtPrdContVO>) beansCopy(queryLmtPrdConts, LmtPrdContVO.class);
			logger.info("根据条件："+ JSON.toJSONString(lmtPrdContVO) +"，查询结果集数量"+ result.size());
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return result;
	}

	/**
	 * 开发人员：【chenxuan】
	 * 创建/修改日期：【2019/6/4】
	 * 修改描述：变更客户经理
	 * @Param [sedSynCusManageVOList]
	 * @return boolean
	 **/
	@Override
	public boolean batchUpdateCusManage(List<SedSynCusManageVO> sedSynCusManageVOList){
		logger.info("批量更新客户经理开始" );
		boolean result = false;
		try {
			@SuppressWarnings("unused")
			List<SedSynCusManageVO> list = new ArrayList<SedSynCusManageVO>();//
			@SuppressWarnings("unused")
			int num = lmtPrdContDao.batchUpdateCusManage(sedSynCusManageVOList);
			result =true;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("客户经理变更更新授信协议表出错!", e);
		}
		return result;
	}

	@Override
	public int countLmtProContByCertTypeAndCertNo(String certType, String certNo) {
		return lmtPrdContDao.countLmtProContByCertTypeAndCertNo(certType,certNo);
	}

	@Override
	public List<LmtPrdContVO> queryAllLmtPrdCont(LmtPrdContVO lmtPrdContVO){
		logger.info("当前根据证件类型、证件号码查询的参数为："+ JSON.toJSONString(lmtPrdContVO));
		List<LmtPrdContVO> list = new ArrayList<>();
		LmtPrdCont lmtPrdCont = new LmtPrdCont();
		beanCopy(lmtPrdContVO,lmtPrdCont);
		try {
			List<LmtPrdCont> lmtPrdConts = lmtPrdContDao.queryAllLmtPrdCont(lmtPrdCont);
			if (Objects.isNull(lmtPrdConts)) {
				logger.info("根据证件类型、证件号码查询的结果集为空。");
				return null;
			}
			logger.info("当前查询本人所属数据信息的结果集数量为:"+ lmtPrdConts.size());
			list = (List<LmtPrdContVO>) beansCopy(lmtPrdConts, LmtPrdContVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}
	@Override
	public List<LmtPrdContVO> queryByLmtSeqs(@Param("list") List<String> list){

		List<LmtPrdCont> lmtPrdConts = this.lmtPrdContDao.queryByLmtSeqs(list);
		try {
			return (List<LmtPrdContVO>) beansCopy(lmtPrdConts,LmtPrdContVO.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

    @Override
    public List<LmtPrdContVO> aplcOCMExstdCrLmt(LmtPrdContVO lmtPrdContVO) {
		List<LmtPrdContVO> lmtPrdContVOS = new ArrayList<>();
		try {
			lmtPrdContVOS= (List<LmtPrdContVO>) beansCopy(lmtPrdContDao.aplcOCMExstdCrLmt(beanCopy(lmtPrdContVO, new LmtPrdCont())),LmtPrdContVO.class);
        } catch (Exception e) {
            logger.error("【授信协议表】查询同一证件号的所有有效额度信息出错:", e);
        }
        return lmtPrdContVOS;
    }

	@Override
	public int updateLmtStatus(String lmtStatus, String lmtContNo) {
		return lmtPrdContDao.updateLmtStatus(lmtStatus, lmtContNo);
	}
}
